package org.shop.controller.common;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.net.ftp.FTPClient;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.shop.util.CommonUtil;
import org.shop.util.FtpUtil2;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class GetManagerAndWebPhotoController {

	private static Logger logger = LogManager.getLogger(GetManagerAndWebPhotoController.class);
	
	
	
    /**
     * 获取公共图片
     * @param req
     * @return
     */
    @RequestMapping("common/getGoodsPhoto")
    public void getCommonPhoto(HttpServletRequest req,HttpServletResponse res){
    	String photoStr=req.getParameter("photoUrl");
        OutputStream os =null;
        InputStream is =null;
        FTPClient ftpClient =null;
        try {
            res.setContentType("image/jpg");
            ftpClient=FtpUtil2.connect(CommonUtil.getPropertiesParam("ftp_address"), Integer.parseInt(CommonUtil.getPropertiesParam("ftp_port")), CommonUtil.getPropertiesParam("ftp_user"), CommonUtil.getPropertiesParam("ftp_password"));
            if(photoStr.lastIndexOf("/")>0){
                FtpUtil2.changeWorkingDirectory(photoStr.substring(0, photoStr.lastIndexOf("/")), ftpClient);
                is=FtpUtil2.retrieveFileStream(photoStr.substring(photoStr.lastIndexOf("/")+1,photoStr.length()), ftpClient);
            }else{
                is=FtpUtil2.retrieveFileStream(photoStr, ftpClient);
            }
            os=res.getOutputStream();
            byte[] buf = new byte[1024];
            while (true) {
                int read = 0;
                if (is != null) {
                    read = is.read(buf);
                }
                if (read == -1) {
                    break;
                }
                os.write(buf, 0, read);
            }
            os.flush();
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                if(ftpClient!=null){
                    FtpUtil2.closeConnection(ftpClient);
                }
            } catch (Exception e) {
                ftpClient=null;
            }
            try {
                os.close();
            } catch (IOException e) {
                os=null;
            }
            try {
                is.close();
            } catch (IOException e) {
                is=null;
            }
        }
    }
	
	
	
	
	/**
	 * 获取图片
	 * @param req
	 * @return
	 */
	@RequestMapping("manager/getApplyPhoto")
	public void getManagerPhoto(HttpServletRequest req,HttpServletResponse res){
		String photoStr=req.getParameter("photoUrl");
		OutputStream os =null;
		InputStream is =null;
		FTPClient ftpClient =null;
		try {
			res.setContentType("image/jpg");
			ftpClient=FtpUtil2.connect(CommonUtil.getPropertiesParam("ftp_address"), Integer.parseInt(CommonUtil.getPropertiesParam("ftp_port")), CommonUtil.getPropertiesParam("ftp_user"), CommonUtil.getPropertiesParam("ftp_password"));
			if(photoStr.lastIndexOf("/")>0){
				FtpUtil2.changeWorkingDirectory(photoStr.substring(0, photoStr.lastIndexOf("/")), ftpClient);
				is=FtpUtil2.retrieveFileStream(photoStr.substring(photoStr.lastIndexOf("/")+1,photoStr.length()), ftpClient);
			}else{
				is=FtpUtil2.retrieveFileStream(photoStr, ftpClient);
			}
			os=res.getOutputStream();
			byte[] buf = new byte[1024];
            while (true) {
                int read = 0;
                if (is != null) {
                    read = is.read(buf);
                }
                if (read == -1) {
                    break;
                }
                os.write(buf, 0, read);
            }
            os.flush();
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			try {
				if(ftpClient!=null){
					FtpUtil2.closeConnection(ftpClient);
				}
			} catch (Exception e) {
				ftpClient=null;
			} 
			try {
				os.close();
			} catch (IOException e) {
				os=null;
			} 
			try {
				is.close();
			} catch (IOException e) {
				is=null;
			}
		}
	}
	/**
	 * 获取图片
	 * @param req
	 * @return
	 */
	@RequestMapping("web/getApplyPhoto")
	public void getWebPhoto(HttpServletRequest req,HttpServletResponse res){
		String photoStr=req.getParameter("photoUrl");
		OutputStream os =null;
		InputStream is =null;
		FTPClient ftpClient =null;
		try {
			res.setContentType("image/jpg");
			ftpClient=FtpUtil2.connect(CommonUtil.getPropertiesParam("ftp_address"), Integer.parseInt(CommonUtil.getPropertiesParam("ftp_port")), CommonUtil.getPropertiesParam("ftp_user"), CommonUtil.getPropertiesParam("ftp_password"));
			if(photoStr.lastIndexOf("/")>0){
				FtpUtil2.changeWorkingDirectory(photoStr.substring(0, photoStr.lastIndexOf("/")), ftpClient);
				is=FtpUtil2.retrieveFileStream(photoStr.substring(photoStr.lastIndexOf("/")+1,photoStr.length()), ftpClient);
			}else{
				is=FtpUtil2.retrieveFileStream(photoStr, ftpClient);
			}
			os=res.getOutputStream();
			byte[] buf = new byte[1024];
			while (true) {
				int read = 0;
				if (is != null) {
					read = is.read(buf);
				}
				if (read == -1) {
					break;
				}
				os.write(buf, 0, read);
			}
			os.flush();
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			try {
				if(ftpClient!=null){
					FtpUtil2.closeConnection(ftpClient);
				}
			} catch (Exception e) {
				ftpClient=null;
			} 
			try {
				os.close();
			} catch (IOException e) {
				os=null;
			} 
			try {
				is.close();
			} catch (IOException e) {
				is=null;
			}
		}
	}
	
	/**
     * 获取供应商商品图片
     * @param req
     * @return
     */
    @RequestMapping("supplier/getApplyPhoto")
    public void getGoodsPhoto(HttpServletRequest req,HttpServletResponse res){
        String photoStr=req.getParameter("photoUrl");
        OutputStream os =null;
        InputStream is =null;
        FTPClient ftpClient =null;
        try {
            res.setContentType("image/jpg");
            ftpClient=FtpUtil2.connect(CommonUtil.getPropertiesParam("ftp_address"), Integer.parseInt(CommonUtil.getPropertiesParam("ftp_port")), CommonUtil.getPropertiesParam("ftp_user"), CommonUtil.getPropertiesParam("ftp_password"));
            if(photoStr.lastIndexOf("/")>0){
                FtpUtil2.changeWorkingDirectory(photoStr.substring(0, photoStr.lastIndexOf("/")), ftpClient);
                is=FtpUtil2.retrieveFileStream(photoStr.substring(photoStr.lastIndexOf("/")+1,photoStr.length()), ftpClient);
            }else{
                is=FtpUtil2.retrieveFileStream(photoStr, ftpClient);
            }
            os=res.getOutputStream();
            byte[] buf = new byte[1024];
            while (true) {
                int read = 0;
                if (is != null) {
                    read = is.read(buf);
                }
                if (read == -1) {
                    break;
                }
                os.write(buf, 0, read);
            }
            os.flush();
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                if(ftpClient!=null){
                    FtpUtil2.closeConnection(ftpClient);
                }
            } catch (Exception e) {
                ftpClient=null;
            }
            try {
                os.close();
            } catch (IOException e) {
                os=null;
            }
            try {
                is.close();
            } catch (IOException e) {
                is=null;
            }
        }
    }
	
    /**
     * 获取自营商品图片
     * @param req
     * @return
     */
    @RequestMapping("autotrophy/getGoodsPhoto")
    public void getAutotrophyPhoto(HttpServletRequest req,HttpServletResponse res){
    	String photoStr=req.getParameter("photoUrl");
    	OutputStream os =null;
    	InputStream is =null;
    	FTPClient ftpClient =null;
    	try {
    		res.setContentType("image/jpg");
    		ftpClient=FtpUtil2.connect(CommonUtil.getPropertiesParam("ftp_address"), Integer.parseInt(CommonUtil.getPropertiesParam("ftp_port")), CommonUtil.getPropertiesParam("ftp_user"), CommonUtil.getPropertiesParam("ftp_password"));
    		if(photoStr.lastIndexOf("/")>0){
    			FtpUtil2.changeWorkingDirectory(photoStr.substring(0, photoStr.lastIndexOf("/")), ftpClient);
    			is=FtpUtil2.retrieveFileStream(photoStr.substring(photoStr.lastIndexOf("/")+1,photoStr.length()), ftpClient);
    		}else{
    			is=FtpUtil2.retrieveFileStream(photoStr, ftpClient);
    		}
    		os=res.getOutputStream();
    		byte[] buf = new byte[1024];
    		while (true) {
    			int read = 0;
    			if (is != null) {
    				read = is.read(buf);
    			}
    			if (read == -1) {
    				break;
    			}
    			os.write(buf, 0, read);
    		}
    		os.flush();
    	} catch (Exception e) {
    		e.printStackTrace();
    	}finally {
    		try {
    			if(ftpClient!=null){
    				FtpUtil2.closeConnection(ftpClient);
    			}
    		} catch (Exception e) {
    			ftpClient=null;
    		}
    		try {
    			os.close();
    		} catch (IOException e) {
    			os=null;
    		}
    		try {
    			is.close();
    		} catch (IOException e) {
    			is=null;
    		}
    	}
    }
    
}
